package ninja.awesome.game.main;

import ninja.awesome.game.MainGameScreen;
import ninja.awesome.game.NinjaGame;
import ninja.awesome.game.gameobject.Boss;

public class Timeline {
	private int level;
	private long startTime;
	private int phase;
	private long lastSpawn;
	private boolean isBossKilled = false;
	private long bossKilledTime;
	private long allRobotsKilledTime;
	private boolean isAllRobotsKilled = false;
	private boolean isWin = false;

	public Timeline(int level) {
		this.level = level;
		setStartTime(System.currentTimeMillis());
		setLastSpawn(0);
		setPhase(-1);
	}

	public void setLevel(int level) {
		this.level = level;
		setStartTime(System.currentTimeMillis());
		setLastSpawn(0);
		setPhase(-1);
	}

	public void run(Controller c, MainGameScreen g, int ticks, boolean bossSpawn, int totalKilled) {
		// define the level of the timeline to run. For example, if the user
		// chooses level 1, we run level 1's timeline
		long gameDuration = (System.currentTimeMillis() - getStartTime()) / 1000;
		if (level == 1) {
			switch (getPhase()) {
			case -1:
				c.spawnRandomEnemy(4);
				setLastSpawn(gameDuration);
				setPhase(0);
				break;
			case 0:
				if (getLastSpawn() < gameDuration && gameDuration - getLastSpawn() == 10) {
					c.spawnRandomEnemy(4);
					setLastSpawn(gameDuration);
				}
				// If user kill all robot, we spawn one more
				if (c.getEnemyList().size() == 0) {
					c.spawnRandomEnemy(1);
				}
				// after 1 minute, go to next phase
				if (gameDuration > 60) {
					setPhase(1);
				}
				break;
			case 1:
				if (getLastSpawn() < gameDuration && gameDuration - getLastSpawn() == 12) {
					c.spawnRandomEnemy(6);
					setLastSpawn(gameDuration);
				}
				// If user kill all robot, we spawn one more
				if (c.getEnemyList().size() == 0) {
					c.spawnRandomEnemy(1);
				}
				if (gameDuration > 120) {
//					setPhase(2);
					setLevel(2);
				}
				break;
			case 2:
//				if (getLastSpawn() < gameDuration && gameDuration - getLastSpawn() == 14) {
//					c.spawnRandomEnemy(8);
//					setLastSpawn(gameDuration);
//				}
//				// If user kill all robot, we spawn one more
//				if (c.getEnemyList().size() == 0) {
//					c.spawnRandomEnemy(1);
//				}
//				if (gameDuration > 180) {
//					setLevel(2);
//				}
//				break;
			default:
				break;
			}
		} else if (level == 2) {
			switch (getPhase()) {
			case -1:
				c.spawnRandomEnemy(10);
				setLastSpawn(gameDuration);
				setPhase(0);
				break;
			case 0:
				if (getLastSpawn() < gameDuration && gameDuration - getLastSpawn() == 10) {
					c.spawnRandomEnemy(10, 10);
					setLastSpawn(gameDuration);
				}
				// If user kill all robot, we spawn one more
				if (c.getEnemyList().size() == 0) {
					c.spawnRandomEnemy(1);
				}
				if (gameDuration > 60) {
					setPhase(1);
				}
				break;
			case 1:
				if (getLastSpawn() < gameDuration && gameDuration - getLastSpawn() == 12) {
					c.spawnRandomEnemy(10, 20);
					setLastSpawn(gameDuration);
				}
				// If user kill all robot, we spawn one more
				if (c.getEnemyList().size() == 0) {
					c.spawnRandomEnemy(1);
				}
				if (gameDuration > 120) {
					setLevel(3);
				}
				break;
			default:
				break;
			}
		} else if (level == 3) {
			switch (phase) {
			case -1:
				c.spawnRandomEnemy(5, 20);
				Boss boss = new Boss(NinjaGame.WIDTH - 250, 10, c.game, c, c.s);
				c.getBossList().add(boss);
				setPhase(0);
				break;
			case 0:
				if (c.getBossList().size() == 0 && !isBossKilled) {
					isBossKilled = true;
					bossKilledTime = System.currentTimeMillis();
				}
				if (c.getEnemyList().size() == 0 && !isAllRobotsKilled) {
					isAllRobotsKilled = true;
					allRobotsKilledTime = System.currentTimeMillis();
				}
				if (isBossKilled && isAllRobotsKilled) {
					// win
					isWin = true;
				} else {
					if (isBossKilled && System.currentTimeMillis() - bossKilledTime > 5000) {
						c.getBossList().add(new Boss(NinjaGame.WIDTH - 250, 10, c.game, c, c.s));
						isBossKilled = false;
					}
					if (isAllRobotsKilled && System.currentTimeMillis() - allRobotsKilledTime > 5000) {
						c.spawnRandomEnemy(5, 20);
						isAllRobotsKilled = false;
					}
				}
				break;
			default:
				break;
			}
		}
	}

	public int getPhase() {
		return phase;
	}

	public void setPhase(int phase) {
		this.phase = phase;
	}

	public long getLastSpawn() {
		return lastSpawn;
	}

	public void setLastSpawn(long lastSpawn) {
		this.lastSpawn = lastSpawn;
	}

	public long getStartTime() {
		return startTime;
	}

	public void setStartTime(long startTime) {
		this.startTime = startTime;
	}

	public int getLevel() {
		return level;
	}

	public boolean isWin() {
		return isWin;
	}

}
